home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dialog / diaetc.c < prev    next >
C/C++ Source or Header  |  1996-07-31  |  4KB  |  199 lines

  1. #define __DIALOG_MAIN__
  2.  
  3.  
  4. #include "diadef.h"
  5. #include "dialog.h"
  6. #ifdef HAVE_NCURSES
  7.     #include "colors.h"
  8. #endif
  9.  
  10. static char is_init = 0;
  11. /*
  12.     Clean up at the end of the program.
  13.     Called automaticly by atexit().
  14. */
  15. void dialog_end ()
  16. {
  17.     if (is_init){
  18.         endwin();
  19.         is_init = 0;
  20.     }
  21. }
  22.  
  23.  
  24. /*
  25.  * Do some initialization for dialog
  26.  */
  27. void init_dialog(void)
  28. {
  29.     if (dialog_mode == DIALOG_CURSES
  30.         && !is_init){
  31.         static char atexit_init = 0;
  32.         is_init = 1;
  33.         if (!atexit_init){
  34.             atexit (dialog_end);
  35.             atexit_init = 1;
  36.         }
  37.         #ifdef HAVE_NCURSES
  38.         if (parse_rc() == -1)    /* Read the configuration file */
  39.             exit(-1);
  40.         #endif
  41.  
  42.         initscr();     /* Init curses */
  43.         keypad(stdscr, TRUE);
  44.         cbreak();
  45.         noecho();
  46.  
  47.         #ifdef HAVE_NCURSES
  48.             if (use_colors || use_shadow)    /* Set up colors */
  49.                 color_setup();
  50.         #endif
  51.  
  52.         /* Set screen to screen attribute */
  53.         attr_clear(stdscr, LINES, COLS, screen_attr);
  54.         wnoutrefresh(stdscr);
  55.     }
  56. }
  57.  
  58.  
  59. #ifdef HAVE_NCURSES
  60. /*
  61.  * Setup for color display
  62.  */
  63. void color_setup(void)
  64. {
  65.     if (has_colors()) {    /* Terminal supports color? */
  66.         start_color();
  67.  
  68.         // Initialize color pairs
  69.         int i;
  70.         for (i = 0; i < ATTRIBUTE_COUNT; i++)
  71.             init_pair(i+1, color_table[i][0], color_table[i][1]);
  72.  
  73.         // Setup color attributes
  74.         for (i = 0; i < ATTRIBUTE_COUNT; i++)
  75.             attributes[i] = C_ATTR(color_table[i][2], i+1);
  76.     }
  77. }
  78. /* End of color_setup() */
  79. #endif
  80.  
  81.  
  82. /*
  83.  * Set window to attribute 'attr'
  84.  */
  85. void attr_clear(WINDOW *win, int height, int width, chtype attr)
  86. {
  87.     if (dialog_mode != DIALOG_CURSES) return;
  88.  
  89.     wattrset(win, attr);    /* Set window to attribute 'attr' */
  90.     for (int i = 0; i < height; i++) {
  91.         wmove(win, i, 0);
  92.         for (int j = 0; j < width; j++)
  93.             waddch(win, ' ');
  94.     }
  95.     touchwin(win);
  96. }
  97.  
  98. /*
  99.  * Print a button
  100.  */
  101. void print_button(WINDOW *win, const char *label, int y, int x, int selected)
  102. {
  103.     draw_box(win, y-1, x, 3, strlen(label)+2, dialog_attr
  104.         , border_attr, border_attr_shadow);
  105.     wmove(win, y, x+1);
  106.     int temp = strspn(label, " ");
  107.     label += temp;
  108.     wattrset(win, selected ? button_label_active_attr : button_label_inactive_attr);
  109.     for (int i = 0; i < temp; i++) waddch(win, ' ');
  110.     wattrset(win, selected ? button_key_active_attr : button_key_inactive_attr);
  111.     waddch(win, label[0]);
  112.     wattrset(win, selected ? button_label_active_attr : button_label_inactive_attr);
  113.     waddstr(win, (char*)(label+1));
  114.     wmove(win, y, x+temp+1);
  115. }
  116. /* End of print_button() */
  117.  
  118.  
  119. /*
  120.  * Draw a rectangular box with line drawing characters
  121.  */
  122. void draw_box(
  123.     WINDOW *win,
  124.     int y,
  125.     int x,
  126.     int height,
  127.     int width,
  128.     chtype box,
  129.     chtype border_light,    // Receiving the light source
  130.     chtype border_shadow)    
  131. {
  132.     wattrset(win, 0);
  133.     for (int i = 0; i < height; i++) {
  134.         wmove(win, y + i, x);
  135.         for (int j = 0; j < width; j++){
  136.             if (!i && !j)
  137.                 waddch(win, border_light | ACS_ULCORNER);
  138.             else if (i == height-1 && !j)
  139.                 waddch(win, border_light | ACS_LLCORNER);
  140.             else if (!i && j == width-1)
  141.                 waddch(win, border_shadow | ACS_URCORNER);
  142.             else if (i == height-1 && j == width-1)
  143.                 waddch(win, border_shadow | ACS_LRCORNER);
  144.             else if (!i)
  145.                 waddch(win, border_light | ACS_HLINE);
  146.             else if (i == height-1)
  147.                 waddch(win, border_shadow | ACS_HLINE);
  148.             else if (!j)
  149.                 waddch(win, border_light | ACS_VLINE);
  150.             else if (j == width-1)
  151.                 waddch(win, border_shadow | ACS_VLINE);
  152.             else
  153.                 waddch(win, box | ' ');
  154.         }
  155.     }
  156. }
  157. /* End of draw_box() */
  158.  
  159.  
  160. /*
  161.  * Draw shadows along the right and bottom edge to give a more 3D look
  162.  * to the boxes
  163.  */
  164. #if defined(HAVE_NCURSES) && 0
  165. void draw_shadow(WINDOW *win, int y, int x, int height, int width)
  166. {
  167.     if (use_shadow
  168.         && has_colors()) {    /* Whether terminal supports color? */
  169.         wattrset(win, shadow_attr);
  170.         int bottomy = y + height;
  171.         // This code does not work anymore and crash on ELF systems
  172.         // Don't know why yet! The winch() macro seg fault. With this
  173.         // patch, it works and look not so bad
  174.         #define my_winch(w)    ' '
  175.         if (bottomy < LINES){
  176.             wmove(win, bottomy, x + 4);
  177.             for (int i = 0; i < width; i++)
  178.                 waddch(win, my_winch(win) & A_CHARTEXT);
  179.         }else{
  180.             bottomy = LINES - 1;
  181.         }
  182.         int lastx = x + width;
  183.         if (lastx < COLS){
  184.             for (int i = y + 2; i <= bottomy; i++) {
  185.                 wmove(win, i, lastx);
  186.                 waddch(win, my_winch(win) & A_CHARTEXT);
  187.                 waddch(win, my_winch(win) & A_CHARTEXT);
  188.             }
  189.         }
  190.         #undef my_winch
  191.         wnoutrefresh(win);
  192.     }
  193. }
  194. #else
  195. void draw_shadow(WINDOW *, int, int, int , int)
  196. {
  197. }
  198. #endif
  199.